<?php
class sortAction extends runAction {
	
	private $s = '_sort';
	private $position = array ();
	private $del_arr = array ();
	
	public function index() {
		
		$this->assign ( 'tag_title', $_REQUEST ['tag_title'] );
		
		$s = $_REQUEST ['s']?$_REQUEST ['s']:$_REQUEST ['ss'];
		$o = M ( $s . $this->s );
		$this->assign ( 's', $s );
		
		$fid = $_REQUEST ['fid'] == '' ? 0 : $_REQUEST ['fid'];
		$this->assign ( 'fid', $fid );
		
		$where = "1 AND fid = $fid";
		
		$kw = $_REQUEST ['kw'];
		
		if ($kw && $kw != '请输入关键字') {
			$where .= " AND title LIKE '%$kw%'";
		}
		$this->assign ( 'kw', $kw );
		
		$o->where ( $where );
		
		$total = $o->count ();
		$this->assign ( 'total', $total );
		
		//$o->field ( 'id,fid,hit,pri,display,title' );
		
		$o->where ( $where );
		
		$order = $_REQUEST ['order'];
		if ($order == '') {
			$order = 'pri';
		}
		$this->assign ( 'order', $order );
		
		$by = $_REQUEST ['by'];
		if ($by == '') {
			$by = 'DESC';
		}
		$o->order ( "$order $by, id DESC" );
		$this->assign ( 'by', $by );
		
		$ps = $_REQUEST ['ps'];
		if ($ps == '') {
			$ps = '10';
		}
		$o->limit ( $ps );
		$this->assign ( 'ps', $ps );
		$this->assign ( 'ptotal', ceil ( $total / $ps ) );
		
		$p = $_REQUEST ['p'];
		if ($p == '') {
			$p = '1';
		}
		$o->Page ( $p );
		$this->assign ( 'p', $p );
		
		$list = $o->select ();
		$this->assign ( 'list', $list );
		//		dump($o);
		

		$this->position1 ( $fid );
		$this->assign ( 'position', $this->position );
		
		$this->display ( MODULE_NAME . ':index' );
	}
	
	public function del() {
		
		if (count ( $_POST ['id'] ) == 0) {
			$this->assign ( 'act_word', '没有选中任何记录' );
			$this->index ();
			exit ();
		}
		
		//搜索子分类
		foreach ($_POST ['id'] as $k => $v){
			$this->del_tree($v);
			$this->del_arr[]=$v;
		}
		$del_str = implode(',',$this->del_arr);
		
		//删除父级和所有子分类
		$o = M ( $_REQUEST ['s'] . $this->s );
		$o->where ( "id IN ($del_str)" )->delete ();
		
		//删除附件
		import ( 'app.ORG.file' );	
		foreach ($this->del_arr as $k => $v){
			file::del_dir ( 'upload/' . $_REQUEST ['s'] . $this->s . '/' . $v );
		}
		
		//回收ID
		$o = M ( $_REQUEST ['s'] . $this->s );
		$o->query ( 'ALTER TABLE __TABLE__ AUTO_INCREMENT = 1' );
		
		//$this->assign('jumpUrl','/adminiii.php/sort/index/s/'.$_REQUEST['s'].'/fid.'.$_REQUEST['fid']);
		$this->assign ( 'act_word', '操作成功 : 已删除 ' . count($this->del_arr) . ' 条记录 ( 包含所属子分类 )' );
		$this->index ();
	}
	
	public function clean() {
		
		import ( 'app.ORG.file' );
		if($_REQUEST['fid'] != 0){
			$this->del_tree($_REQUEST['fid']);
			$del_str = implode(',',$this->del_arr);
			
			//删除父级和所有子分类
			$o = M ( $_REQUEST ['s'] . $this->s );
			$o->where ( "id IN ($del_str)" )->delete ();
			
			//删除附件
			foreach ($this->del_arr as $k => $v){
				file::del_dir ( 'upload/' . $_REQUEST ['s'] . $this->s . '/' . $v );
			}
			
		}else{
			//删除附件
			file::del_dir ( 'upload/' . MODULE_NAME . '/', 0 );
			
			$o = M ( $_REQUEST ['s'] . $this->s );
			$o->query ( "TRUNCATE TABLE __TABLE__" );
		}
		
		$this->assign ( 'act_word', '清空成功' );
		$this->index ();
	}
	
	/*public function pri() {
		
		$num = 0;
		$o = M ( MODULE_NAME );
		foreach ( $_POST ['ids'] as $k => $v ) {
			
			if ($_POST ['pri'] [$k] != $_POST ['opri'] [$k]) {
				
				$d ['pri'] = $_POST ['pri'] [$k];
				$o->where ( 'id = ' . $v )->save ( $d );
				$num ++;
			}
		}
		
		if ($num > 0) {
			$this->success ( '已更新 ' . $num . ' 条记录' );
		} else {
			$this->error ( '没有记录被更新' );
		}
	
	}
	*/
	public function onoff() {
		
		if (count ( $_POST ['id'] ) == 0) {
			
			$this->error ( '没有选中任何记录' );
		}
		if ($_POST ['value'] == '') {
			
			$this->error ( '未指定开关' );
		}
		
		list ( $zd, $onoff ) = explode ( ',', $_POST ['value'] );
		
		$in = '';
		foreach ( $_POST ['id'] as $k => $v ) {
			if ($v != '') {
				if ($k != 0) {
					$in .= ',';
				}
				$in .= $v;
			}
		}
		
		$row [$zd] = $onoff;
		$o = M ( $_REQUEST ['s'] . $this->s );
		$o->where ( "id IN ($in)" )->save ( $row );
		
		$this->assign ( 'act_word', '操作成功 : 已更新 ' . mysql_affected_rows () . ' 条记录' );
		$this->index();
		//$this->success ( '已更新 ' . mysql_affected_rows () . ' 条记录' );
	}
	
	public function view() {
		
		CK ( 'uploaddir', $_REQUEST ['s'] . $this->s );
		
		if (empty ( $_GET ['id'] )) {
			
			//add
			$v = array ('hit' => 0, 'pri' => 0, 'display' => 1, 's1' => $_GET ['s1'], 's2' => $_GET ['s2'], 's3' => $_GET ['s3'], 'bs1' => $_GET ['bs1'] );
		} else {
			
			//edit
			$o = M ( $_REQUEST ['s'] . $this->s );
			$v = $o->getById ( $_GET ['id'] );
		}
		$v ['s'] = $_REQUEST ['s'];
		$this->assign ( 'v', $v );
		
		$this->assign ( 'fid', $_GET ['fid'] );
		
		$this->display ();
	}
	
	public function save() {
		
		import ( 'app.ORG.ztime' );
		
		if ($_POST ['id'] == '') {
			
			//add
			$o = M ( $_REQUEST ['s'] . $this->s );
			$data = $_POST;
			$data['hot'] = $_POST ['hot'] == 1 ? 1 : 0;
			//$data['posttime'] = ztime::datetime2line ( $_POST ['date'] . ' ' . $_POST ['time'] );
			

			$st = $o->data ( $data )->add ();
			
			if ($st !== false) {
				
				$GLOBALS ['id'] = mysql_insert_id ();
				mk_dir ( 'upload/' . $_REQUEST ['s'] . $this->s . '/' . $GLOBALS ['id'] );
				
				foreach ( $_FILES as $key => $val ) {
					if ($val ['tmp_name']) {
						$v = explode ( '.', $val ['name'] );
						$_FILES [$key] ['name'] = $key . '.' . $v [count ( $v ) - 1];
					}
				}
				
				$upload = A (upload);
				$upload -> upload();
				
				//$this->assign ( 'way', 'close' );
				$_POST['fid']=0;
				$this->success ( '添加成功' );
			} else {
				$this->error ( '添加失败' );
			}
		
		} else {
			
			//edit
			$o = M ( $_REQUEST ['s'] . $this->s );
			
			$data = $_POST;
			$data['hot'] = $_POST ['hot'] == 1 ? 1 : 0;
			//$data['posttime'] = ztime::datetime2line ( $_POST ['date'] . ' ' . $_POST ['time'] );
			

			$st = $o->data ( $data )->save ();
			
			if ($st !== false) {
				
				$GLOBALS ['id'] = $_POST ['id'];
				
				foreach ( $_FILES as $key => $val ) {
					if ($val ['tmp_name']) {
						$v = explode ( '.', $val ['name'] );
						$_FILES [$key] ['name'] = $key . '.' . $v [count ( $v ) - 1];
					}
				}
				
				$upload = A (upload);
					$upload -> upload();
				
				//$this->assign ( 'way', 'close' );
				$_POST['fid']=0;
				$this->success ( '修改成功' );
			} else {
				
				$this->error ( '修改失败' );
			}
		}
	}
	
	public function aj_sort() {
		$o = M ( $_GET ['s'] . $this->s );
		$o->where ( 'fid=' . $_GET ['fid'] );
		$list = $o->select ();
		
		if (count ( $list ) != 0 && $list != false) {
			$this->assign ( 'check', $_GET ['check'] );
			$this->assign ( 'lv', ++ $_GET ['lv'] );
			$this->assign ( 's', $_GET ['s'] );
			$this->assign ( 'id', $_GET ['sname'] );
			$this->assign ( 'list', $list );
			$this->display ();
		}
	}
	
	public function s() {
		
		$o = M ( $_REQUEST ['s'] . $this->s );
		$list = $o->select ();
		
		$list2 = array ();
		foreach ( $list as $key => $v ) {
			$list2 [$v ['fid']] [] = $v;
		}
		//dump($list2);
		header("Content-type: text/html; charset=utf-8");
		$this->print_s ( 0, $list2, 1 );
	
	}
	
	private function print_s($i, $list2, $k, $arr) {
		$i == '' ? 0 : $i;
		echo '<ul>' . "\r\n";
		if ($i == 0) {
			//echo '<li><a href="'.__APP__.'/'.$_REQUEST['s'].'/index'.'/word/'.rawurlencode('全部').'">全部</a></li>';
			echo '<li><a href="javascript:clear_sort(' . "'" . $_REQUEST [name] . "'" . ')">全部</a></li>';
		}
		foreach ( $list2 [$i] as $v ) {
			echo '<li>' . "\r\n";
			$arr [$_REQUEST ['name'] . $k] = $v ['id'];
			//$s = '';
			$key_arr='';
			$vv_arr='';
			foreach ( $arr as $key => $vv ) {
				//$s[] = $s.'fw%5B'.$key.'%5D='.$vv;
				$key_arr [] = $key;
				$vv_arr [] = $vv;
			}
			$str = "'" . implode ( $key_arr, ',' ) . "','" . implode ( $vv_arr, ',' ) . "','word_$_REQUEST[name],$v[title]'";
			//echo '<a href="'.__APP__.'/'.$_REQUEST['s'].'/index/?'.$str.'&word='.rawurlencode($v['title']).'">'.$v['title'].'</a>'."\r\n";
			echo '<a href="javascript:sort_search(' . $str . ')">' . $v ['title'] . '</a>' . "\r\n";
			
			if (count ( $list2 [$v ['id']] ) > 0) {
				$k ++;
				$this->print_s ( $v ['id'], $list2, $k, $arr );
			}
			
			echo '</li>' . "\r\n";
			if ($i == 0) {
				$arr = NULL;
				$k = 1;
			}
		}
		echo '</ul>' . "\r\n";
	}
	
	private function position1($id) {
		$o = M ( $_REQUEST ['s'] . $this->s );
		$o->where ( 'id = ' . $id );
		$var = $o->find ();
		
		if (! is_array ( $var )) {
			return 1;
		} else {
			$this->position [] = $var;
			$this->position1 ( $var ['fid'] );
		}
	}
	
	private function del_tree($id) {
		$o = M ( $_REQUEST ['s'] . $this->s );
		$o->where('fid = '.$id);
		$var = $o->select();
		
		if(!is_array($var)){
			return 1;
		}else{
			foreach( $var as $v){
				$this->del_arr[] = $v['id'];
				$this->del_tree($v['id']);
			}
		}
	}

}
?>